home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / applet / applet~2.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  6.1 KB  |  247 lines

  1. /*
  2.  * @(#)Applet.java    1.29 95/12/14 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.applet;
  20.  
  21. import java.awt.*;
  22. import java.awt.image.ColorModel;
  23. import java.net.URL;
  24. import java.net.MalformedURLException;
  25.  
  26. /**
  27.  * Base applet class. 
  28.  *
  29.  * @version     1.29, 12/14/95
  30.  *
  31.  * @author Chris Warth
  32.  * @author Arthur van Hoff
  33.  */
  34. public class Applet extends Panel {
  35.     private AppletStub stub;
  36.  
  37.     /**
  38.      * Sets the applet stub. This is done by automatically by the system.
  39.      */
  40.     public final void setStub(AppletStub stub) {
  41.     this.stub = (AppletStub)stub;
  42.     }
  43.  
  44.     /**
  45.      * Returns true if the applet is active. An applet is marked active
  46.      * just before the start method is called.
  47.      * @see #start
  48.      */
  49.     public boolean isActive() {
  50.     return stub.isActive();
  51.     }
  52.     
  53.     /**
  54.      * Gets the document URL. This is the URL of the document in which
  55.      * the applet is embedded.
  56.      * @see #getCodeBase
  57.      */
  58.     public URL getDocumentBase() {
  59.     return stub.getDocumentBase();
  60.     }
  61.  
  62.     /**
  63.      * Gets the base URL. This is the URL of the applet itself. 
  64.      * @see #getDocumentBase
  65.      */
  66.     public URL getCodeBase() {
  67.     return stub.getCodeBase();
  68.     }
  69.  
  70.     /**
  71.      * Gets a parameter of the applet.
  72.      */
  73.      public String getParameter(String name) {
  74.      return stub.getParameter(name);
  75.      }
  76.  
  77.     /**
  78.      * Gets a handle to the applet context. The applet context
  79.      * lets an applet control the applet's environment which is
  80.      * usually the browser or the applet viewer.
  81.      */
  82.     public AppletContext getAppletContext() {
  83.     return stub.getAppletContext();
  84.     }
  85.  
  86.     /**
  87.      * Requests that the applet be resized.
  88.      */
  89.     public void resize(int width, int height) {
  90.     Dimension d = size();
  91.     if ((d.width != width) || (d.height != height)) {
  92.         super.resize(width, height);
  93.         if (stub != null) {
  94.         stub.appletResize(width, height);
  95.         }
  96.     }
  97.     }
  98.  
  99.     /**
  100.      * Requests thatthe applet be resized.
  101.      */    
  102.     public void resize(Dimension d) {
  103.     resize(d.width, d.height);
  104.     }
  105.  
  106.     /**
  107.      * Shows a status message in the applet's context.
  108.      */
  109.     public void showStatus(String msg) {
  110.     getAppletContext().showStatus(msg);
  111.     }
  112.  
  113.     /**
  114.      * Gets an image given a URL. Note that this method
  115.      * always returns an image object immediatly, even if 
  116.      * the image does not exist. The actual image data is 
  117.      * loaded when it is first needed.
  118.      */
  119.     public Image getImage(URL url) {
  120.     return getAppletContext().getImage(url);
  121.     }
  122.  
  123.     /**
  124.      * Gets an image relative to a URL. This methods returns
  125.      * immediately, even if the image does not exist. The actual
  126.      * image data is loaded when it is first needed.
  127.      * 
  128.      * @see #getImage
  129.      */
  130.     public Image getImage(URL url, String name) {
  131.     try {
  132.         return getImage(new URL(url, name));
  133.     } catch (MalformedURLException e) {
  134.         return null;
  135.     }
  136.     }
  137.  
  138.     /**
  139.      * Gets an audio clip. 
  140.      */
  141.     public AudioClip getAudioClip(URL url) {
  142.     return getAppletContext().getAudioClip(url);
  143.     }
  144.  
  145.     /**
  146.      * Gets an audio clip. 
  147.      * @see #getAudioClip
  148.      */
  149.     public AudioClip getAudioClip(URL url, String name) {
  150.     try {
  151.         return getAudioClip(new URL(url, name));
  152.     } catch (MalformedURLException e) {
  153.         return null;
  154.     }
  155.     }
  156.  
  157.     /**
  158.      * Returns a string containing information about
  159.      * the author, version and copyright of the applet.
  160.      */
  161.     public String getAppletInfo() {
  162.     return null;
  163.     }
  164.  
  165.     /**
  166.      * Returns an array of strings describing the
  167.      * parameters that are understood by this
  168.      * applet. The array consists of sets of three strings:
  169.      * name/type/description. For example:
  170.      * <pre>
  171.      *     String pinfo[][] = {
  172.      *      {"fps",    "1-10",    "frames per second"},
  173.      *      {"repeat", "boolean", "repeat image loop"},
  174.      *      {"imgs",   "url",     "directory in which the images live"}
  175.      *    };
  176.      * </pre>
  177.      */
  178.     public String[][] getParameterInfo() {
  179.     return null;
  180.     }
  181.  
  182.     /**
  183.      * Plays an audio clip. Nothing happens if the audio clip could
  184.      * not be found.
  185.      */
  186.     public void play(URL url) {
  187.     AudioClip clip = getAudioClip(url);
  188.     if (clip != null) {
  189.         clip.play();
  190.     }
  191.     }
  192.  
  193.     /**
  194.      * Plays an audio clip. Nothing happens if the audio clip could
  195.      * not be found.
  196.      */
  197.     public void play(URL url, String name) {
  198.     AudioClip clip = getAudioClip(url, name);
  199.     if (clip != null) {
  200.         clip.play();
  201.     }
  202.     }
  203.  
  204.     /**
  205.      * Initializes the applet.
  206.      * You never need to call this directly, it is called automatically
  207.      * by the system once the applet is created.
  208.      * @see #start
  209.      * @see #stop
  210.      * @see #destroy
  211.      */
  212.     public void init() {
  213.     }
  214.  
  215.     /**
  216.      * Called to start the applet. You never need to call this method
  217.      * directly, it is called when the applet's document is visited.
  218.      * @see #init
  219.      * @see #stop
  220.      * @see #destroy
  221.      */
  222.     public void start() {
  223.     }
  224.  
  225.     /**
  226.      * Called to stop the applet. It is called when the applet's document is
  227.      * no longer on the screen. It is guaranteed to be called before destroy()
  228.      * is called. You never need to call this method directly.
  229.      * @see #init
  230.      * @see #start
  231.      * @see #destroy
  232.      */
  233.     public void stop() {
  234.     }
  235.  
  236.     /**
  237.      * Cleans up whatever resources are being held. If the applet is active
  238.      * it is stopped stopped.
  239.  
  240.      * @see #init
  241.      * @see #start
  242.      * @see #stop
  243.      */
  244.     public void destroy() {
  245.     }
  246. }
  247.